package Calculator;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

/**
 * @author Administrator
 *
 */
public class Calculator extends JFrame implements ActionListener {
	private static final long serialVersionUID = 201314;
	TextField display;
	JButton button;
	JButton reset;
	double result = 0.0;
	String operator = "=";
	boolean isFirstDigit = true;
	boolean operateValidFlag = true;
	
	Font f = new Font("正楷", Font.LAYOUT_LEFT_TO_RIGHT, 40);

	public Calculator() {
		setTitle("计算器");
		setBounds(800, 100, 500, 400);
		setVisible(true);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		JPanel top = new JPanel();
		top.setLayout(new BorderLayout());
		display = new TextField("0");
		display.setFont(new Font("正楷", Font.LAYOUT_LEFT_TO_RIGHT, 35));
		display.setEnabled(false);
		top.add(display);
		reset = new JButton("CE");
		reset.setFont(new Font("正楷", Font.LAYOUT_LEFT_TO_RIGHT, 35));
		top.add(reset, BorderLayout.EAST);
		JPanel center = new JPanel();
		center.setLayout(new GridLayout(4, 4));
		String[] str = { "7", "8", "9", "+", "4", "5", "6", "-", "1", "2", "3", "*", "0", ".", "=", "/" };
		for (String s : str) {
			button = new JButton(s);
			button.setFont(f);
			center.add(button);
			button.addActionListener(this);
		}
		add(center);
		add(top, BorderLayout.NORTH);

		display.addActionListener(this);
		reset.addActionListener(this);

	}

	public static void main(String[] args) {

		SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
				new Calculator();
			}
		});
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		String lable = e.getActionCommand();
		if ("0123456789.".indexOf(lable) >= 0)
			handleNumber(lable);
		else if (lable.equals("CE"))
			handleReset();
		else
			handleOperator(lable);
	}

	public void handleNumber(String key) {

		if (isFirstDigit) {
			display.setText(key);
		} else if ((key.equals(".")) && (display.getText().indexOf(".") < 0)) {
			display.setText(display.getText() + ".");
		} else if (!key.equals(".")) {
			display.setText(display.getText() + key);
		}
		isFirstDigit = false; // 以后输入的肯定不是第一个数字了
	}

	private void handleOperator(String key) {
		
		if (operator.equals("+"))
			result += Double.valueOf(display.getText());
		else if (operator.equals("-"))
			result -= Double.valueOf(display.getText());
		else if (operator.equals("*"))
			result *= Double.valueOf(display.getText());
		else if (operator.equals("/")) {
			if (Double.valueOf(display.getText()).doubleValue()== 0.0) {
				display.setText("除数不能为零");
			} else
				result /= Double.valueOf(display.getText());
		} else if (operator.equals("="))
			result = Double.valueOf(display.getText());
		if (operateValidFlag) { // 双精度浮点数的运算
			long t1;
			double t2;
			t1 = (long) result;
			t2 = result - t1;
			if (t2 == 0) {
				display.setText(String.valueOf(t1));
			} else {
				display.setText(String.valueOf(result));
			}
		}
		operator = key;
		isFirstDigit = true;
		operateValidFlag = true;
		

	}

	private void handleReset() {
		display.setText("0");
		isFirstDigit = true;
		
		operator = "=";
		operateValidFlag = true;

	}

}
